package ch.uzh.ifi.sopra.gruppe9.game.junit;

import ch.uzh.ifi.sopra.gruppe9.text_pdk.*;

import junit.framework.*;
import static org.junit.Assert.*;
import java.rmi.RemoteException;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Random;

public class GameBoardTest extends TestCase {

	private static IGameBoard myBoard;
	
	public GameBoardTest() throws RemoteException {
		super();
	}
	
	public void testPlayersInGame() throws RemoteException {
		TestBoard myBoard = new TestBoard();
		myBoard.registerPlayer();
		assertEquals(1,myBoard.getPlayerNum());	
		myBoard.registerPlayer();
		assertEquals(2,myBoard.getPlayerNum());
	}
	
	public void testgetSeason() throws RemoteException {
		TestBoard myBoard = new TestBoard();
		assertTrue(myBoard.getSeason() == 0);
	}
	public void testStartGameNotReady() throws RemoteException {
		TestBoard myBoard = new TestBoard();
		assertNotNull(myBoard);  //GameBoard was instantiated
		myBoard.startGame();
		myBoard.registerPlayer();
		assertNotNull(myBoard.getPlayers());
		assertEquals(1,myBoard.getPlayerNum());
		assertFalse(myBoard.getPlayers().get(0).isReady());
	}
	
	public void testStartGameReady() throws RemoteException {
		TestBoard myBoard = new TestBoard();
		myBoard.registerPlayer();
		myBoard.registerPlayer();
		assertFalse(myBoard.isInGame());  //Players are not yet ready
		for(int i = 0; i < myBoard.getPlayerNum(); i++)
			myBoard.getPlayer(i).setReady(true);
		
		myBoard.startGame(myBoard.getPlayers());
		
		assertTrue(myBoard.isInGame());  //now players should be ready
		int[] myindex = myBoard.getPlayer(0).getBuyingAgentIDs();
		assertNotNull(myindex.toString());  //players must have buying agents assigned
	}
	
	public void testgetProtege() throws RemoteException {
		TestBoard myBoard = new TestBoard();
		assertTrue(myBoard.getProtege() < 2);
	}
	
	public void testgetTurn() throws RemoteException {
		TestBoard myBoard = new TestBoard();
		assertTrue(myBoard.getTurn() < 4);
	}
	
	public void testIsEndOfSeason() throws RemoteException {
		TestBoard myBoard = new TestBoard();
		myBoard.registerPlayer();
		myBoard.registerPlayer();
		myBoard.startGame(myBoard.getPlayers());
		assertTrue(myBoard.getSeason()<4);
	}
	
	public void testEndOfGame() throws RemoteException {
		TestBoard myBoard = new TestBoard();
		myBoard.registerPlayer();
		myBoard.registerPlayer();
		for(int i = 0; i < myBoard.getPlayerNum(); i++)
			myBoard.getPlayer(i).setReady(true);
		
		myBoard.startGame(myBoard.getPlayers());
		assertTrue(myBoard.getSeason()<2);
		myBoard.nextPlayersTurn(myBoard.getPlayers());
		assertTrue(myBoard.getSeason()<2);
		assertFalse(myBoard.getPlayer(0).usedAllBuyingAgents());
		assertFalse(myBoard.getPlayer(1).usedAllBuyingAgents());
		
		//last but not least, need to setup inventory for players and test evaluation
		ArrayList<IPlayer> score = new ArrayList<IPlayer>();
		//score = myBoard.endOfGame(myBoard.getPlayers()); fails - players without towers to compare!
		
		//fill in players inventory
		myBoard.getPlayer(0).addElementToInventory(new TowerElement(0));
		myBoard.getPlayer(1).addElementToInventory(new TowerElement(3));
		
		//fill in players tower inventory
		ArrayList<Tower> player1Towers = myBoard.getPlayer(0).getTowers();
		Tower new1Tower = new Tower();
		new1Tower.assemble("0,20,65");
		player1Towers.add(new1Tower);
		assertNotNull(myBoard.getPlayer(0).getTowers());
		
		ArrayList<Tower> player2Towers = myBoard.getPlayer(1).getTowers();
		Tower new2Tower = new Tower();
		new2Tower.assemble("3,23,68");
		player2Towers.add(new2Tower);
		assertNotNull(myBoard.getPlayer(1).getTowers());
		
		//score = myBoard.endOfGame(myBoard.getPlayers()); //wenn nicht alle Spieler min. je einen Turm pro Farbe haben -> Null Pointer Exception!
	}
}
